package com.kuaiying.kuai.ying.model.task_id;

import java.util.*;

import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.kuaiying.kuai.ying.model.leaflet.LeafletModel;
import com.kuaiying.kuai.ying.model.print_file.PrintfileModel;

public class TaskidModel extends Model<TaskidModel>{

	private static final long serialVersionUID = -6984293973714614241L;
	
	public static final TaskidModel me = new TaskidModel();
	
	private static final String USER_CACHE = "task_id";
	
	public TaskidModel(){
		super( );
	}
	/**
	 * 所有 sql 与业务逻辑写在 Model 或 Service 中，不要写在 Controller 中，养成好习惯，有利于大型项目的开发与维护
	 */
	public Page<TaskidModel> paginate(int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from  task_id order by id asc");
	}
	
	/**
	 * 根據用戶名和用戶密碼驗證用戶是否存在
	 * @param userName：用戶名
	 * @param pwd：用戶密碼
	 * @return：用戶信息或null
	 */
	public TaskidModel getUserByNameAndPwd(String userName, String pwd){
		String sql = "select * from task_id where name = '" + userName + "' and password ='" +  pwd + "'" ;
		System.out.println( sql );
		
		TaskidModel user = me.findFirst(sql);
		
		return user;
	}

	public boolean updateTask(String name,Integer user_id,Integer gold,Integer print_shop_id,Integer file_id,String papertype,Integer papernum,String beizhu,String submit_time){
		TaskidModel task = new TaskidModel();
		task.set("name", name);
		task.set("user_id", user_id);
		task.set("gold", gold);
		task.set("print_shop_id", print_shop_id);
		task.set("file_id", file_id);
		task.set("papertype", papertype);
		task.set("papernum", papernum);
		task.set("beizhu", beizhu);
		task.set("submit_time", submit_time);
		task.set("finish_flag", 0);
		return task.save();
	}
	
	public boolean update_flag(Integer taskid){
		TaskidModel tasknew = TaskidModel.me.findById(taskid);
		tasknew.set("finish_flag",1);
		return tasknew.update();
	}
	
	public List<TaskidModel> getAllTask_shop(Integer printshop_id){
		String ss= Integer.toString(printshop_id);
		String sql = "select * from task_id where finish_flag = 0 and print_shop_id = "+Integer.toString(printshop_id);
		System.out.println( sql );
		List<TaskidModel> tasks  = me.find(sql);
		return tasks;
	}
	
	//分页测试
	public List<TaskidModel> getAllTask_shop_finish_flag_page(Integer printshop_id, Integer finish_flag,int pageNumber){
		String sql = "from task_id where finish_flag = " + Integer.toString(finish_flag) + " and print_shop_id = "+Integer.toString(printshop_id);
		Page<TaskidModel> Page_TasksModel = paginate(pageNumber, 10, "select *", sql + " order by task_id asc");
		List<TaskidModel> tasks  = Page_TasksModel.getList();
		return tasks;
	}
	
	public List<TaskidModel> getAllTask_shop_finish_flag(Integer printshop_id, Integer finish_flag){
		String ss= Integer.toString(printshop_id);
		String sql = "select * from task_id where finish_flag = " + Integer.toString(finish_flag) + " and print_shop_id = "+Integer.toString(printshop_id);
		System.out.println( sql );
		List<TaskidModel> tasks  = me.find(sql);
		return tasks;
	}
	
	public List<TaskidModel> getAllTask_printshop_finish_flag(Integer printshop_id, Integer finish_flag){
		String ss= Integer.toString(printshop_id);
		String sql = "select * from task_id where finish_flag = " + Integer.toString(finish_flag) + " and print_shop_id = "+Integer.toString(printshop_id);
		System.out.println( sql );
		List<TaskidModel> tasks  = me.find(sql);
		return tasks;
	}
	
	public List<TaskidModel> getAllTask_user(Integer user_id){
		String ss= Integer.toString(user_id);
		String sql = "select * from task_id where finish_flag = 0 and user_id = "+Integer.toString(user_id);
		System.out.println( sql );
		List<TaskidModel> tasks  = me.find(sql);
		return tasks;
	}
	
	public List<TaskidModel> getAllTask_user_page(Integer user_id,int pageNumber){
		String sql = "from task_id where finish_flag = 0 and user_id = "+Integer.toString(user_id);
		Page<TaskidModel> Page_TasksModel = paginate(pageNumber, 10, "select *", sql + " order by task_id asc");
		List<TaskidModel> tasks  = Page_TasksModel.getList();
		return tasks;
	}
	
	public List<TaskidModel> getAllTask_printShop(Integer printShop_id){
		String ss= Integer.toString(printShop_id);
		String sql = "select * from task_id where finish_flag = 0 and print_shop_id = "+Integer.toString(printShop_id);
		System.out.println( sql );
		List<TaskidModel> tasks  = me.find(sql);
		return tasks;
	}
	
	public List<TaskidModel> getAllTask_printShop_page(Integer printShop_id,int pageNumber){
		String sql = "from task_id where finish_flag = 0 and print_shop_id = "+Integer.toString(printShop_id);
		Page<TaskidModel> Page_TasksModel = paginate(pageNumber, 10, "select *", sql + " order by task_id asc");
		List<TaskidModel> tasks  = Page_TasksModel.getList();
		return tasks;
	}
	
	public List<Integer> getFinishLeaftel(Integer user_id){
		String sql = "select * from task_id where user_id = "+Integer.toString(user_id);
		System.out.println( sql );
		Set set=new HashSet();
		List<TaskidModel> tasks = me.find(sql);
		for(Iterator i = tasks.iterator();i.hasNext(); ){
			TaskidModel task = (TaskidModel) i.next();
			set.add(task.getInt("leaflet_id"));
		}
		List<Integer> leaflet_ids = new ArrayList();
		for(Iterator i = set.iterator();i.hasNext(); ){
			leaflet_ids.add((Integer) i.next());
		}
		return leaflet_ids;
	}
	
}


